package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"shang/conmon"
	"shang/models"
	"strconv"
	"time"
)

type LoginController struct {
	beego.Controller
}

func (l *LoginController) Login() {
	username := l.GetString("username")
	password := l.GetString("password")
	user, _ := models.GetUserInfo(models.User12{
		Username: username,
	})
	if user.Id == 0 {
		l.Data["json"] = map[string]interface{}{
			"code":  400,
			"msg":   "账号不存在",
			"token": "",
		}
		l.ServeJSON()
		return
	}
	m := conmon.Md5(password)
	if user.Password != m {
		l.Data["json"] = map[string]interface{}{
			"code":  400,
			"msg":   "密码错误",
			"token": "",
		}
		l.ServeJSON()
		return
	}
	token, _ := conmon.GetJwtToken("shangxinping", time.Now().Unix(), 3600, strconv.Itoa(user.Id))
	if token == "" {
		l.Data["json"] = map[string]interface{}{
			"code":  400,
			"msg":   "token生成失败",
			"token": "",
		}
		l.ServeJSON()
		return
	}
	l.Data["json"] = map[string]interface{}{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	}
	l.ServeJSON()
	return
}
